/**
 * fork 并完成exec
 * @author: sundx
 * @date: 2020-05-30 22:04:32
 * @Last Modified by:   sundx
 * @Last Modified time: 2020-05-30 22:04:32
 */
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
int main() {
  pid_t pid = fork();
  if (pid == -1) {
    std::cout << "faild fork" << std::endl;
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    int ret;
    int fd = open("1.txt", O_CREAT | O_RDWR, 0664);
    if (fd == -1) {
      std::cout << "open file failed" << std::endl;
      exit(EXIT_FAILURE);
    }
    // ret = close(STDOUT_FILENO);
    // if (ret == -1) {
    //   std::cout << "close out failed" << std::endl;
    //   exit(EXIT_FAILURE);
    // }
    ret = dup2(fd, STDOUT_FILENO);
    if (ret == -1) {
      std::cout << "dup failed" << std::endl;
      exit(EXIT_FAILURE);
    }
    execlp("ps", "ps", "aux", NULL);
    std::cout << "error" << std::endl;
  } else {
    std::cout << "parent" << std::endl;
  }
  return 0;
}